import store from './store'
import { useEffect, useReducer } from "react";

export default function Pre06Redux() {

    console.log({ store })
    // useReducer初始化函数中不能写成v++,只能写成v+1
    const [, forceUpdateDispatch] = useReducer(v => v + 1, 0)

    useEffect(() => {
        // 在这里订阅store，当有数据变化时刷新视图
        store.subscribe(() => {
            forceUpdateDispatch() // 相当于class组件中的this.forceUpdate()，也可在入口index.js中订阅
        })
    }, [])

    return <div>
        <h3>redux</h3>
        <div>获取的state的值：{store.getState()}</div>
        <button onClick={() => store.dispatch({ type: 'ADD' })}>add</button>
        <button onClick={() => store.dispatch({ type: 'MINUS' })}>minus</button>
    </div>

}